package jvm;

import java.io.*;

class MyClassLoader01 extends ClassLoader{
    private String baseDir;
    public MyClassLoader01(String baseDir){
        this.baseDir=baseDir;
    }

    @Override
    protected Class<?> findClass(String name) throws ClassNotFoundException{
        byte[] classDate = loadClassBytes(name);
        if (classDate == null){
            throw new ClassNotFoundException();
        }else {
            return defineClass(name,classDate,0,classDate.length);
        }
    }

    //自己定义
    private byte[] loadClassBytes(String className){
        String fileName = baseDir+className.replace('.', File.separatorChar)+".class";
        System.out.println("fileName="+fileName);
        InputStream ins = null;
        try{
            ins = new FileInputStream(fileName);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            int bufferSize=1024;
            byte[] buffer = new byte[bufferSize];
            int length = 0;
            while((length=ins.read(buffer))!=-1){
                baos.write(buffer,0,length);
            }
            return baos.toByteArray();
        }catch (IOException e){
            e.printStackTrace();
            throw new RuntimeException(e);
        }finally {
            if (ins!=null)
                try{ins.close();}catch (Exception e){}
        }
    }
}

public class TestMyClassLoader01 {

    public static void main(String[] args) throws Exception{
        String baseDir = "D:\\yh\\workspace\\";
        MyClassLoader01 classLoader = new MyClassLoader01(baseDir);
        //此类不要和当前类放相同目录结构中
        String pkgCls ="pkg.Search";//包名.类名
        Class<?> testClass = classLoader.loadClass(pkgCls);//不会执行静态代码块
        Object object = testClass.newInstance();//静态代码块在执行构造方法之前执行
        System.out.println(object.getClass());
        System.out.println(object.getClass().getClassLoader());
    }

}
